package youleche.com.controler.servlet;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import youleche.com.business.BFixInfo;
import youleche.com.common.ParamConfigParameters;
import youleche.com.controler.servlet.common.UlecheHttpServlet;
import youleche.com.factory.MLLogFactory;
import youleche.com.model.MFixInfo;
import youleche.com.util.AESEncryptAndDecrypt;
import youleche.com.util.DateUtil;
import youleche.com.util.JsonHelper;

/**
 * Servlet修理信息serblet
 */
public class SFixInfoServlet extends UlecheHttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SFixInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int action = Integer.parseInt(request.getParameter("action"));
		switch(action){
			case 0:
				//发布修理信息
				this.releaseFixInfo(request, response);
				break;
			case 1:
				//得到更多修理信息
				this.getFixInfoList(request, response);
				break;
			case 2:
				//根据ID得到修理内容
				this.getFixInfoById(request, response);
				break;
		}
	}
	
	/**
	 * 根据ID得到修理内容
	 * @param request
	 * @param response
	 */
	private void getFixInfoById(HttpServletRequest request, HttpServletResponse response){
		try {
			String fixId = request.getParameter("id");
			BFixInfo handler = new BFixInfo();
			MFixInfo fix = handler.getFixInfoById(fixId);
			request.setAttribute("fix", fix);
			request.getRequestDispatcher("/fixdetail.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			super.response(response, "");
		} 
		
		super.response(response, "");
	}
	
	/**
	 * 发布修理信息
	 * @param request
	 * @param response
	 */
	private void getFixInfoList(HttpServletRequest request, HttpServletResponse response){
		try {
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));
			Integer startIndex = (Integer.parseInt(request.getParameter("pageIndex")) - 1) * pageSize ;
			BFixInfo handler = new BFixInfo();
			List<MFixInfo> list = handler.getFixInfos(startIndex.toString(), ((Integer)pageSize).toString());
			JsonHelper.printListToJsonString(response, list);
		} catch (Exception e) {
			e.printStackTrace();
			super.response(response, "");
		} 
		
		super.response(response, "");
	}
	
	/**
	 * 发布修理信息
	 * @param request
	 * @param response
	 */
	private void releaseFixInfo(HttpServletRequest request, HttpServletResponse response){
		try {
			MFixInfo fixInfo = (MFixInfo) JsonHelper.getObjectByJsonString(request, MFixInfo.class);
			ParamConfigParameters paramConfig = new ParamConfigParameters();
			String key = paramConfig.getParameter("LinkNum_KEY");
			fixInfo.setPhoneNum(AESEncryptAndDecrypt.encrypt(fixInfo.getPhoneNum(), key));
			fixInfo.setAddTime(DateUtil.getNowDate());
			fixInfo.setIsDelete("0");
			BFixInfo handler = new BFixInfo();
			MLLogFactory.exceptionLog("添加售后信息出错maaaaaaaaaaaaa", "添加售后信息出错", "aaaa", request.getRemoteAddr());
			if(handler.addFixInfo(fixInfo)>0){
				super.response(response, "1");
				return;
			}
		} catch (Exception e) {

			MLLogFactory.exceptionLog("添加售后信息出错", "添加售后信息出错", e.getMessage(), request.getRemoteAddr());
			super.response(response, "0");
		} 
		
		super.response(response, "0");
	}

}
